﻿using System;
using Medicalsystem.MCP.FormDesigner.Common;

namespace Medicalsystem.MCP.FormDesigner
{
    /// <summary>
    /// 控件的移动管理
    /// </summary>
    public class MoveHandler
    {
        private System.Windows.Forms.Control _owner;
        private System.Windows.Forms.Control _controller;

        private int _mouseClickAtX = 0;
        private int _mouseClickAtY = 0;

        private CtrlMngDesign _ctrlMngDesign;

        /// <summary>
        /// 控件移动管理器
        /// </summary>
        /// <param name="ctrllHandler">主体-控件管理者</param>
        /// <param name="controller">控制者</param>
        public MoveHandler(CtrlMngDesign ctrlMngDesign, System.Windows.Forms.Control controller)
        {
            this._owner = ctrlMngDesign.Owner;
            this._controller = controller;
            this._ctrlMngDesign = ctrlMngDesign;

            this._controller.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Owner_MouseDown);
            this._controller.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Owner_MouseMove);
            this._controller.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Owner_MouseUp);

            this._controller.Cursor = System.Windows.Forms.Cursors.SizeAll;
        }

        /// <summary>
        /// 关联的控件
        /// </summary>
        public CtrlMngDesign ctrlMngDesign
        {
            get { return _ctrlMngDesign; }
        }

        #region 私有方法

        private void Owner_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                this._ctrlMngDesign.ModelDesign.SelectListAdd(_ctrlMngDesign);

                this._mouseClickAtX = e.X;
                this._mouseClickAtY = e.Y;

                if (!this._ctrlMngDesign.ModelDesign.CtrlKeyDown)
                {
                    _ctrlMngDesign.ParentContainer.SetControlKnob(null);

                    this._ctrlMngDesign.ModelDesign.BeginMove = true;
                }

                //刷新模板和组件容器-消除选取框
                this._ctrlMngDesign.ParentContainer.Container.Refresh();
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        private void Owner_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                if (this._ctrlMngDesign.ModelDesign.BeginMove)
                {
                    this._ctrlMngDesign.ModelDesign.PropertyChangeAvail = false;

                    ControlMove(e.X - this._mouseClickAtX, e.Y - this._mouseClickAtY);

                    this._ctrlMngDesign.ModelDesign.PropertyChangeAvail = true;
                }
            }
            catch { }
        }

        private void Owner_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                if (this._ctrlMngDesign.ModelDesign.BeginMove)
                {
                    this._ctrlMngDesign.ModelDesign.BeginMove = false;

                    _ctrlMngDesign.ParentContainer.SetControlKnob(_ctrlMngDesign);

                    this._ctrlMngDesign.ParentContainer.Container.Refresh();
                }
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        /// <summary>
        /// 处理控件移动
        /// </summary>
        /// <param name="moveOffsetX"></param>
        /// <param name="moveOffsetY"></param>
        public void ControlMove(int moveOffsetX, int moveOffsetY)
        {
            if (!SingleList.Instance.MoveLock)
            {
                this._ctrlMngDesign.ParentContainer.Container.SuspendLayout();

                foreach (CtrlMngDesign ctrlMngDesign in this._ctrlMngDesign.ModelDesign.SelectList)
                {
                    ctrlMngDesign.Owner.Left += moveOffsetX;
                    ctrlMngDesign.Owner.Top += moveOffsetY;
                }

                this._ctrlMngDesign.ParentContainer.Container.ResumeLayout();
            }
        }

        #endregion 私有方法
    }
}